package com.tind.wyf.baselib.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import com.tind.wyf.baselib.utils.log.LogUtils;

import java.io.Serializable;
import java.util.Map;

public class ToActivity {


    private Class<?> mClass;
    private Bundle bundle;
    private int requestCode;

    private ToActivity(ToActivity.Builder builder) {
        this.bundle = builder.bundle;
        this.mClass = builder.mClass;
        this.requestCode = builder.requestCode;
    }

    public static ToActivity.Builder with(Context context) {
        return new ToActivity.Builder(context);
    }

    /**
     * Activity 跳转
     */
    private void toActivity(Activity activity) {
        Intent in = new Intent();
        in.setClass(activity, mClass);
        in.putExtras(bundle);
        if (requestCode == -1) {
            activity.startActivity(in);
        } else {
            activity.startActivityForResult(in, requestCode);
        }

    }

    public static class Builder {
        private static final String TAG = "ToActivity";

        private Context context;
        private Activity activity;
        private Class<?> mClass;
        private Bundle bundle = null;

        private int requestCode = -1;

        Builder(Context context) {
            this.context = context;
        }

        private ToActivity build() {
            return new ToActivity(this);
        }

        public Builder setActivity(Activity activity) {
            LogUtils.i(TAG, "setActivity: " + activity.getLocalClassName());
            this.activity = activity;
            return this;
        }

        public Builder setClass(@NonNull Class<?> toClass) {
            mClass = toClass;
            return this;
        }

        /**
         * 设置跳转请求code
         *
         * @param code
         * @return
         */
        public Builder setRequestCode(int code) {
            requestCode = code;
            return this;
        }

        /**
         * 传map数据
         *
         * @param map
         * @return
         */
        public Builder setMap(Map<String, Object> map) {
            bundle = putBundle(null, map);
            return this;
        }

        /**
         * 传map数据
         *
         * @param bundle
         * @return
         */
        public Builder setBundle(Bundle bundle) {
            this.bundle = bundle;
            return this;
        }

        /**
         * 传bundle数据
         * 传map数据
         *
         * @param bundle
         * @param map
         * @return
         */
        public Builder setData(Bundle bundle, Map<String, Object> map) {
            this.bundle = putBundle(bundle, map);
            return this;
        }

        /**
         * 进行跳转
         */
        public void toActivity() {
            this.build().toActivity(activity);
        }

        /**
         * 跳转数据装载
         *
         * @param bundle
         * @param map
         * @return
         */
        private Bundle putBundle(Bundle bundle, Map<String, Object> map) {
            if (null == bundle) {
                bundle = new Bundle();
            }
            if (null != map && map.size() > 0) {
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    Object value = entry.getValue();
                    String key = entry.getKey();
                    if (value instanceof Byte) {
                        bundle.putByte(key, (byte) value);
                    } else if (value instanceof Short) {
                        bundle.putShort(key, (Short) value);
                    } else if (value instanceof Integer) {
                        bundle.putInt(key, (int) value);
                    } else if (value instanceof Long) {
                        bundle.putLong(key, (long) value);
                    } else if (value instanceof Float) {
                        bundle.putFloat(key, (float) value);
                    } else if (value instanceof Double) {
                        bundle.putDouble(key, (double) value);
                    } else if (value instanceof Boolean) {
                        bundle.putBoolean(key, (boolean) value);
                    } else if (value instanceof Character) {
                        bundle.putChar(key, (char) value);
                    } else if (value instanceof String) {
                        bundle.putString(key, (String) value);
                    } else if (value instanceof Serializable) {
                        bundle.putSerializable(key, (Serializable) value);
                    } else if (value instanceof Parcelable) {
                        bundle.putParcelable(key, (Parcelable) value);
                    }
                }
            }
            return bundle;
        }

    }

}
